import Vue from 'vue'
import Vuex from 'vuex'
import httpApi from '@/http'
import router from '@/router'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: sessionStorage.getItem('user') ? JSON.parse(sessionStorage.getItem('user')) : null,
  },
  getters: {
  },
  mutations: {
    updateUser(state, payload) {
      state.user = payload
      if (payload) {
        sessionStorage.setItem('user', JSON.stringify(payload))
      } else {
        sessionStorage.removeItem('user')
      }
    }
  },
  actions: {
    login(store, payload) {
      console.log(payload);
      httpApi.loginApi.login(payload).then(res => {
        console.log(res.data.data);
        if (res.data.code == 200) {
          store.commit('updateUser', res.data.data[0])
          router.push('/')
        }
      })
    }
  },
  modules: {
  }
})
